<template>
  <div class="register-page">
    <van-nav-bar
      title="注册"
      left-arrow
      @click-left="router.back()"
    />

    <!-- 身份选择 -->
    <div class="role-select" v-if="step === 1">
      <div class="section-title">请选择注册身份</div>
      <div class="role-grid">
        <div
          v-for="role in roles"
          :key="role.id"
          class="role-card"
          :class="{ active: selectedRole === role.id }"
          @click="selectRole(role.id)"
        >
          <van-icon :name="role.icon" size="32" />
          <span class="role-name">{{ role.name }}</span>
          <span class="role-desc">{{ role.description }}</span>
        </div>
      </div>
      <div class="action-bar">
        <van-button
          type="primary"
          block
          round
          :disabled="!selectedRole"
          @click="step = 2"
        >
          下一步
        </van-button>
      </div>
    </div>

    <!-- 注册表单 -->
    <van-form @submit="onSubmit" v-else>
      <!-- 原有的表单内容 -->
      <van-cell-group inset>
        <van-field
          v-model="formData.username"
          name="username"
          label="用户名"
          placeholder="请输入用户名"
          :rules="[{ required: true, message: '请输入用户名' }]"
        />
        <van-field
          v-model="formData.password"
          type="password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请输入密码' }]"
        />
        <van-field
          v-model="formData.confirmPassword"
          type="password"
          name="confirmPassword"
          label="确认密码"
          placeholder="请再次输入密码"
          :rules="[
            { required: true, message: '请确认密码' },
            { validator: validatePassword, message: '两次输入的密码不一致' }
          ]"
        />
        <van-field
          v-model="formData.realName"
          name="realName"
          label="真实姓名"
          placeholder="请输入真实姓名"
          :rules="[{ required: true, message: '请输入真实姓名' }]"
        />
        <van-field
          v-model="formData.phone"
          name="phone"
          label="手机号"
          placeholder="请输入手机号"
          :rules="[
            { required: true, message: '请输入手机号' },
            { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }
          ]"
        />
      </van-cell-group>

      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          注册
        </van-button>
        <van-button
          round
          block
          plain
          type="primary"
          style="margin-top: 10px"
          @click="step = 1"
        >
          返回选择身份
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { register } from '@/api/auth.js'

const router = useRouter()
const step = ref(1)
const selectedRole = ref(null)

// 身份选项
const roles = [
  {
    id: 3,
    name: '普通用户',
    icon: 'user-o',
    description: '享受在线问诊、健康管理等服务'
  },
  {
    id: 2,
    name: '医生',
    icon: 'manager-o',
    description: '为患者提供专业的医疗咨询服务'
  }
]

const formData = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  realName: '',
  phone: '',
})

// 选择身份
const selectRole = (roleId) => {
  selectedRole.value = roleId
}

// 验证密码
const validatePassword = (val) => {
  return val === formData.password
}

// 提交注册
const onSubmit = async () => {
  try {
    await register({
      ...formData,
      roleId: selectedRole.value
    })
    showToast('注册成功')
    router.push('/login')
  } catch (error) {
    showToast('注册失败：' + error.message)
  }
}
</script>

<style lang="scss" scoped>
.register-page {
  min-height: 100vh;
  background-color: #f7f8fa;
  padding-top: 46px;
}

.role-select {
  padding: 20px;
}

.section-title {
  font-size: 18px;
  font-weight: 500;
  color: #323233;
  margin-bottom: 20px;
  text-align: center;
}

.role-grid {
  display: grid;
  gap: 16px;
  margin-bottom: 24px;
}

.role-card {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  border: 2px solid transparent;

  .van-icon {
    display: block;
    margin: 0 auto 12px;
    color: #666;
  }

  .role-name {
    display: block;
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 8px;
    color: #323233;
  }

  .role-desc {
    display: block;
    font-size: 13px;
    color: #969799;
  }

  &.active {
    border-color: #1989fa;
    background: #e1f0ff;

    .van-icon {
      color: #1989fa;
    }
  }

  &:active {
    opacity: 0.8;
  }
}

.action-bar {
  margin-top: 24px;
}
</style>
